package cn.yunhe.controller;



import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.yunhe.entity.Bed;
import cn.yunhe.entity.Patient;
import cn.yunhe.entity.Suffer;
import cn.yunhe.service.BedService;
//import cn.yunhe.service.BedService;
import cn.yunhe.service.PatientService;
import cn.yunhe.service.SuffService;
import cn.yunhe.util.DateUtil;

@Controller
public class PatientController {
	
	@Autowired
	private PatientService patientService;
	
	@Autowired
	private SuffService suffService;
	
	@Autowired
	private BedService bedService;
	public PatientService getPatientService() {
		return patientService;
	}
	@RequestMapping("/findPatientOne")
	public String findPatientOne(HttpServletRequest request){
		System.out.println("====================住院情况!!!!==============");
		List<Patient> plist = patientService.findPatient();
		System.out.println(plist);
		for (Patient patient : plist) {
			System.out.println(patient.getPtext()+",,"+patient.getSuffer().getSname()+",,"+patient.getBed().getBname());
		}
		request.setAttribute("plist", plist);
		return "patientInfoOne";
	}
	@RequestMapping("/findPatient")
	public String findPatient(HttpServletRequest request){
		System.out.println("====================住院情况!!!!==============");
		List<Patient> plist = patientService.findPatientAll();
		System.out.println(plist);
		for (Patient patient : plist) {
			System.out.println(patient.getPtext()+",,"+patient.getSuffer().getSname()+",,"+patient.getBed().getBname());
		}
		request.setAttribute("plist", plist);
		return "patientInfo";
	}
	
	@RequestMapping("/searchPatient")
	public String searchPatient(String sname,HttpServletRequest request){
		System.out.println("====================搜索!!!!==============");
		List<Patient> plist = patientService.findPatientBySname("%"+sname+"%");
		System.out.println(plist);
		for (Patient patient : plist) {
			System.out.println(patient.getPtext()+",,"+patient.getSuffer().getSname()+",,"+patient.getBed().getBname());
		}
		request.setAttribute("plist", plist);
		return "patientInfo";
	}
	// 异步请求
	@RequestMapping("/ajaxSuffer")
	public void ajaxSuffer(String sname,String sphone,HttpServletResponse response){
		System.out.println("====================获取挂号单!!!!==============");
		try {
			PrintWriter out = response.getWriter();
			System.out.println("sname:"+sname+",,sphone:"+sphone);
			Suffer suffer = suffService.findSufferByName(sname, sphone);
			System.out.println("suffer:::"+suffer);
			if(suffer != null){
				out.write("填写正确");
			}
			if(suffer == null){
				out.write("姓名或手机号错误!");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@RequestMapping("/addPatientOne")
	public String addPatientOne(HttpServletRequest request){
		System.out.println("====================添加住院病人1111111!!!! 先查询剩余床位==============");
		int num = bedService.updateBedBstate();
		System.out.println("状态::num:::"+num);
		List<Patient> plist = patientService.findPatientAll();
		System.out.println(plist);
		int num1 = 0;
		for (Patient patient : plist) {
			if(patient.getBed().getBid() > 0 ){
				System.out.println(",,"+patient.getBed().getBname());
				num1 = bedService.updateBedBybid(patient.getBed().getBid());
				System.out.println("num1:::"+num1);
			}
		}
		
		List<Bed> blist = bedService.findBedAll();
		System.out.println("blist:::"+blist.size());
		for (Bed bed : blist) {
			System.out.println(bed.getBname());
		}
		request.setAttribute("blist", blist);
		
		return "patientAdd";
	}
	
	@RequestMapping("/addPatient")
	public String addPatient(Patient patient,String sphone,String sname){
		System.out.println("====================添加住院病人22222222222!!!!==============");
		Suffer suffer = suffService.findSufferByName(sname, sphone);
		System.out.println("sid::::"+suffer.getSid()+",,bid:::"+patient.getPid());
		patient.setPstate(1);
		patient.setPtime(DateUtil.DateToString(new Date()));
		patient.setSuffer(new Suffer(suffer.getSid()));
		int num = patientService.addPatient(patient);
		// 入住 之后 床的状态值 改为 2
		int num1 = bedService.updateBedBybid(patient.getBed().getBid());
		System.out.println("床位:::num:::"+num1);
		System.out.println("num:::"+num);
		return "redirect:findPatient";
	}
	
	@RequestMapping("/deletaPatientByPid")
	public String addPatient(int pid){
		System.out.println("====================删除住院病人!!!!2222	bed==============");
		Patient patient = patientService.findPatientByPid(pid);
		System.out.println("patient:::"+patient);
		System.out.println("patient:::"+patient.getBed().getBid());
		int num1 = bedService.updateBedBybid2(patient.getBed().getBid());
		int num = patientService.deleteByPstate(pid);
		System.out.println("num1111:::"+num);
		System.out.println("num2222:::"+num1);
		return "redirect:findPatient";
	}
	
	@RequestMapping("/patientAjax")
	public void patientAjax(int bid,HttpServletRequest request,HttpServletResponse response){
		try {
			PrintWriter out = response.getWriter();
			System.out.println("==================ajax 查询 入住状态!!!!===============");
			List<Patient> plist = patientService.findPatientAll();
			boolean flag = false;
			for (Patient p : plist) {
				System.out.println("bid:::"+p.getBed().getBid()+",,bid222:::"+bid);
				if(p.getBed().getBid() == bid){	
					System.out.println("11111111111111");
					out.write("床位已被占用!!!");
					flag = true;
					break;
				}
			}
			if(!flag){
				out.write("选位正确!");
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping("/updatePatient")
	public String updatePatient1(int pid,HttpServletRequest request){
		System.out.println("==========更新1111111111111111111=============");
		Patient patient = patientService.findPatientByPid(pid);
		
		request.setAttribute("patient", patient);
		
		return "patientAlter";
	}
	@RequestMapping("/updatePatient2")
	public String updatePatient2(Patient patient,HttpServletRequest request){
		System.out.println("==========更新2222222222222222222=============");
		System.out.println(patient.getPid()+",,"+patient.getPtext()+",,"+patient.getPtime()+",,"+patient.getSuffer().getSid()+",,"+patient.getBed().getBid());
		patient.setPstate(1);
		// 修改方法
		int num = patientService.updatePatient(patient);
		System.out.println("num:::"+num);
		int num3 = bedService.updateBedBstate();
		System.out.println("状态::num:::"+num3);
		List<Patient> plist = patientService.findPatientAll();
		System.out.println(plist);
		int num4 = 0;
		for (Patient patient1 : plist) {
			if(patient.getBed().getBid() > 0 ){
				System.out.println(",,"+patient1.getBed().getBname());
				num4 = bedService.updateBedBybid(patient1.getBed().getBid());
				System.out.println("num1:::"+num4);
			}
		}
		return "redirect:findPatient";
	}

	
	
	
	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}

	public SuffService getSuffService() {
		return suffService;
	}

	public void setSuffService(SuffService suffService) {
		this.suffService = suffService;
	}
	public BedService getBedService() {
		return bedService;
	}
	public void setBedService(BedService bedService) {
		this.bedService = bedService;
	}
	
	
	
	
	
	
}
